<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="模块名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="模块key" prop="module_key">
        <el-input v-model="ruleForm.module_key"></el-input>
      </el-form-item>
      <el-form-item label="模块描述" prop="describe">
        <el-input type="textarea" v-model="ruleForm.describe"></el-input>
      </el-form-item>

      <el-form-item label="测试人员" prop="testers">
        <el-select
          style="width: 100%;"
          v-model="optionsTestersId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入"
          :remote-method="getTestersInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in testers_info"
            :key="index"
            :label="item.username"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="开发人员" prop="developer">
        <el-select
          style="width: 100%;"
          v-model="optionsDeveloperId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入"
          :remote-method="getDeveloperInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in developer_info"
            :key="index"
            :label="item.username"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属项目" prop="project">
        <el-select
          style="width: 100%;"
          v-model="optionsProjectId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入项目名称"
          :remote-method="getProjectInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in project_info"
            :key="index"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="模块状态" prop="status">
        <el-select v-model="ruleForm.status" placeholder="请选择状态" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsCommon"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>
<script>
  export default {
    name: 'Addmodule',
    data() {
      return {
        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        projects: [], // 项目列表，获取1000条项目数据
        EditUrl: this.$http.moduleURL,  // get编辑的URL
        EditIdStr: 'module_id',  // 模块id字符串
        pushObjName: 'module',

        // 其它表数据,搜索到的全部数据
        project_info: [], // 项目列表，获取1000条项目数据
        developer_info: [], // 开发列表，获取1000条项目数据
        testers_info:[],

        // 编辑和添加选中的数据id
        loading: false,
        optionsProjectId: [],
        optionsDeveloperId: [],
        optionsTestersId:[],

        ruleForm: {
          name: '',
          module_key: '',
          describe: '',
          project: '',
          project_info: '',
          testers: '',
          developer: '',
          testers_info: '',
          developer_info: '',
          status: '',
        },  //
        rules: {
          name: [
            {required: true, message: '请输入名称', trigger: 'blur'},
            {min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur'}
          ],
          describe: [
            {required: false, message: '请输入描述', trigger: 'blur'},
            {min: 0, max: 1000, message: '长度在 0 到 1000 个字符', trigger: 'blur'}
          ],
          project: [
            {required: true, message: '请选择项目', trigger: 'change'}
          ],

          testers: [
            {required: false, message: '测试人员', trigger: 'blur'},
            {min: 0, max: 100, message: '长度在 0 到 100 个字符', trigger: 'blur'}
          ],
          developer: [
            {required: false, message: '开发人员', trigger: 'blur'},
            {min: 0, max: 100, message: '长度在 0 到 100 个字符', trigger: 'blur'}
          ],

          status: [
            {required: true, message: '请选择状态', trigger: 'change'}
          ],
        }
      };
    },
    created() {
      this.getEditData();   // get回将要编辑的数据
      this.optionsCommon = this.getChoices.common; // 获取标准CHOICES
      this.$myTool.getObjList(this, this.$http.projectURL, this.projects);  // 获取项目1000条数据
    },
    methods: {
      submitForm() {
        this.$myTool.submitForm(this)
      },

      // get回将要编辑的数据
      getEditData() {
        this.$myTool.getEditData(this);
      },

      // 项目信息搜索
      getProjectInfo(query) {
        this.$myTool.getObjList(this, this.$http.projectURL, this.project_info, query)
      },

      getDeveloperInfo(query){
        this.$myTool.getObjList(this, this.$http.accountURL, this.developer_info, query)
      },
      getTestersInfo(query){
        this.$myTool.getObjList(this, this.$http.accountURL, this.testers_info, query)
      },

      // get将要编辑的数据
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    },
    // 监听
    watch: {
      'ruleForm.project_info': function (value) {
        if (value) {
          this.optionsProjectId.push(value.id);
          this.project_info.push(value);
        } else {
          this.optionsProjectId = [];
          this.project_info = [];
        }
      },
      optionsProjectId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.project = value[0]
      },
      'ruleForm.developer_info': function (value) {
        if (value) {
          this.optionsDeveloperId.push(value.id);
          this.developer_info.push(value);
        } else {
          this.optionsDeveloperId = [];
          this.developer_info = [];
        }
      },
      optionsDeveloperId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.developer = value[0]
      },

      'ruleForm.testers_info': function (value) {
        if (value) {
          this.optionsTestersId.push(value.id);
          this.testers_info.push(value);
        } else {
          this.optionsTestersId = [];
          this.testers_info = [];
        }
      },
      optionsTestersId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.testers = value[0]
      },
    },
    // 计算属性
    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },

  }
</script>
